﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using AMM.Business.Store;
using AMM.CustomWinControls;
using AMM.Model;
using AMM.Model.Store;
using AMM.UserControls;

namespace AMM.Forms.Store {
    public partial class FrmMaterialInventory : FrmMaterialInventoryBase {

        private int _pageIndex;
        private MaterialInventorySearchModel _searchModel;
        private MaterialInventoryProcessor _productMaterialInventoryProcessor;
        private List<MaterialInventoryViewModel> _productMaterialInventoryList;

        #region Override

        protected override string FunctionCode {
            get { return "AMM1419"; }
        }

        protected override BaseSearchControl SearchControl {
            get { return new MaterialInventorySearchControl(); }
        }

        protected override void Add() {
            this.ResetForms();
            this.ShowEditForm("新增", new MaterialInventory() { ClassStatus = ClassStatus.Added });
            base.Add();
        }

        protected override void Amend() {
            List<MaterialInventoryViewModel> selectedList = this.dgvMaterialInventoryList.GetSelectedRows<MaterialInventoryViewModel>();
            if (selectedList.Count > 1) { this.OnlySelectSingleRecord(); return; }
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            MaterialInventory productMaterialInventory = this._productMaterialInventoryProcessor.FetchMaterialInventoryById(selectedList[0].Key);
            productMaterialInventory.ClassStatus = ClassStatus.Updated;
            this.ShowEditForm("修改", productMaterialInventory);
            base.Amend();
        }

        protected override void Save() {
            base.Save();
        }

        protected override void Delete() {
            List<MaterialInventoryViewModel> selectedList = this.dgvMaterialInventoryList.GetSelectedRows<MaterialInventoryViewModel>();
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._productMaterialInventoryProcessor.Delete(selectedList.Select(x => x.Key).ToArray());
                this.MDIParent.SetStatusInformation("删除成功");
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
                base.Delete();
            });
        }       

        protected override void Search(object sender, FrmSearch.SearchArgs e) {
            this.CreateNewThread(() => {
                if (e.IsRevert) {
                    this._searchModel = new MaterialInventorySearchModel();
                }
                else {
                    this._searchModel = (MaterialInventorySearchModel)e.SearchModel;
                }
            }, () => {
                this.BindMaterialInventoryList();
            });
        }

        #endregion

        public FrmMaterialInventory() {
            InitializeComponent();

            this.dgvMaterialInventoryList.DataGridViewModelType = typeof(MaterialInventoryViewModel);
        }

        #region Private methods

        private bool frmEdit_Edit(object sender, FrmEdit.EditArgs e) {
            bool resultFlag = false;
            MaterialInventory productMaterialInventory = (MaterialInventory)e.EditModel;
            this.PreventCancel = true;
            this.CreateNewThread(() => {
                if (productMaterialInventory.ClassStatus == ClassStatus.Added) {
                    productMaterialInventory.CreatedBy = LoginUserName;
                    this._productMaterialInventoryProcessor.Insert(productMaterialInventory);
                }
                else {
                    productMaterialInventory.ModifiedBy = LoginUserName;
                    this._productMaterialInventoryProcessor.Update(productMaterialInventory);
                }
                resultFlag = true;
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
                if (resultFlag) {
                    this.MDIParent.SetStatusInformation("保存成功");
                    this.FormEdit.Close();
                }
            });

            this.Save();
            return true;
        }

        private void ResetForms() {
            this.BindMaterialInventoryList();
        }

        private void BindMaterialInventoryList() {
            this.CreateNewThread(() => {
                this._productMaterialInventoryList = this._productMaterialInventoryProcessor.FetchMaterialInventoryViewModelList(this._searchModel, this._pageIndex);
            }, () => {
                this.dgvMaterialInventoryList.DataSource = new SortableBindingList<MaterialInventoryViewModel>(this._productMaterialInventoryList);
                this.pceList.Records = this._searchModel.Records;
                this.pceList.DataBind();
            });
        }

        private void ShowEditForm(string title, MaterialInventory productMaterialInventory) {
            this.CreateNewThread(() => {
                Thread.Sleep(100);
            }, () => {
                MaterialInventoryEditControl editControl = new MaterialInventoryEditControl();
                this.FormEdit = new FrmEdit();
                editControl.BaseEditModel = productMaterialInventory;
                editControl.ValidatorForEdit = this.FormEdit.validatorForEdit;
                this.FormEdit.Controls.Add(editControl);
                this.FormEdit.Edit += new FrmEdit.EditEventHandler(frmEdit_Edit);
                this.FormEdit.Text = string.Format("{0}{1}", title, this.Text);
                FrmEntry.LoadAndRunDialog(this.FormEdit);
            });
        }

        #endregion

        private void FrmMaterialInventory_Load(object sender, EventArgs e) {
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._productMaterialInventoryProcessor = new MaterialInventoryProcessor(this.pceList.PageSize);
                this._searchModel = new MaterialInventorySearchModel();
                this._pageIndex = 1;
            }, () => {
                this.WindowState = FormWindowState.Maximized;
                this.ResetForms();
                this.PreventCancel = false;
            });
        }

        private void pceList_Paging(object sender, PaginationArgs e) {
            this._pageIndex = e.PageIndex;
            this.BindMaterialInventoryList();
        }
    }
}
